package com.example.tms.controller;

import java.util.List;

import com.example.tms.common.annotation.LogAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.tms.common.enums.Result;
import com.example.tms.entity.EmployeePerformance;
import com.example.tms.service.EmployeePerformanceService;

/**
 * 员工绩效控制器
 * 提供员工绩效记录的CRUD操作
 */
@RestController
@RequestMapping("/employee-performances")
public class EmployeePerformanceController {
    @Autowired
    private EmployeePerformanceService employeePerformanceService;

    /**
     * 获取所有员工绩效记录
     * @return 包含所有员工绩效记录的结果
     */
    @LogAction("获取所有员工绩效信息")
    @GetMapping("")
    public Result selectAll() {
        List<EmployeePerformance>  list =  employeePerformanceService.selectAll();
        return Result.success(list);
    }

    // GET: 根据主键 ID 获取全部的绩效信息
    /**
     * 根据ID获取员工绩效记录详情
     * @param id 员工绩效记录ID
     * @return 包含绩效记录详情的结果
     */
    @LogAction("根据ID获取员工绩效信息")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        EmployeePerformance employeePerformance = employeePerformanceService.selectById(id);
        return Result.success(employeePerformance);
    }

    //根据员工ID获取员工绩效信息
    /**
     * 根据员工ID获取绩效记录
     * @param empId 员工ID
     * @return 包含该员工所有绩效记录的结果
     */
    @LogAction("根据员工ID获取员工绩效信息")
    @GetMapping("/employees/{empId}")
    public Result selectByEmpId(@PathVariable Integer empId) {
        EmployeePerformance employeePerformance = new EmployeePerformance();
        employeePerformance.setEmpId(empId);
        List<EmployeePerformance>  list =  employeePerformanceService.selectByEmpId(employeePerformance);
        return Result.success(list);
    }

    //根据绩效ID获取员工具体绩效
    /**
     * 根据考核周期ID获取绩效记录
     * @param perId 考核周期ID
     * @return 包含该周期所有绩效记录的结果
     */
    @LogAction("根据考核周期ID获取员工绩效信息")
    @GetMapping("/performances/{perId}")
    public Result selectByPerId(@PathVariable Integer perId) {
        List<EmployeePerformance> list = employeePerformanceService.selectByPerId(perId);
        return Result.success(list);
    }

    // POST: 添加员工绩效信息
    /**
     * 创建新的员工绩效记录
     * @param employeePerformance 包含绩效信息的对象
     * @return 操作结果
     */
    @LogAction("创建新的员工绩效信息")
    @PostMapping("")
    public Result add(@RequestBody EmployeePerformance employeePerformance) {
        int result = employeePerformanceService.insert(employeePerformance);
        return result > 0 ? Result.success() : Result.error("添加失败");
    }

    // PUT: 更新员工具体绩效
    /**
     * 更新员工绩效记录
     * @param id 绩效记录ID
     * @param employeePerformance 包含更新信息的对象
     * @return 操作结果
     */
    @LogAction("更新员工绩效信息")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody EmployeePerformance employeePerformance) {
        employeePerformance.setId(id);
        int result = employeePerformanceService.update(employeePerformance);
        return result > 0 ? Result.success() : Result.error("更新失败");
    }

    // DELETE: 删除员工绩效(软删除)
    /**
     * 软删除员工绩效记录
     * @param id 绩效记录ID
     * @return 操作结果
     */
    @LogAction(value = "删除员工绩效信息", level = "WARN")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int result = employeePerformanceService.deleteById(id);
        return result > 0 ? Result.success() : Result.error("删除失败");
    }

}
